package com.itheima.article.controller;

import com.itheima.article.dto.ArticleInfoDto;
import com.itheima.article.pojo.ApAuthor;
import com.itheima.article.service.ApAuthorService;
import com.itheima.common.enums.HttpCodeEnum;
import com.itheima.common.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
public class ArticleFaginController {

    //注入ApAuthor的service层对象
    @Autowired
    private ApAuthorService apAuthorService;

    /**
     * 远程调用查询作者数据库
     * @param apiUserId 接收远程调用接收用户id
     *@param apiWmUserId 接收远程调用接收自媒体账号id
     * @return 返回查询到的数据给远程调用的方法
     */
    @GetMapping("/api/ApAuthor/{apiUserId}/{apiWmUserId}")
    public ResultVo<ApAuthor> findById(@PathVariable(name = "apiUserId") Long apiUserId,
                                                              @PathVariable(name = "apiWmUserId") Long apiWmUserId){

       ApAuthor apAuthor = apAuthorService.findByUserId(apiUserId,apiWmUserId);
       return ResultVo.ok(apAuthor);
    }

    /**
     * 远程调用添加apAuthor
     * @param apAuthor 接收远程调用返回的参数
     * @return 返回添加是否成功的数据
     */
    @PostMapping("/api/ApAuthor/add")
    public ResultVo<ApAuthor> addWmUser(@RequestBody ApAuthor apAuthor){
        if (apAuthorService.save(apAuthor)) {
            return ResultVo.ok();
        }
        return ResultVo.error(HttpCodeEnum.SERVER_ERROR);
    }

}
